﻿using System.IO;
using UnityEditor;
using UnityEngine;

namespace DefaultNamespace
{
    public class MoveABToSA
    {
        [MenuItem("AB包工具/移动选中资源到StreamingAssets中")]
        private static void MoveABToStreamingAssets()
        {
            Object[] selectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            if (selectedAsset.Length == 0) return;
            //用于拼接本地默认AB包资源信息的字符串
            string abCompareInfo = "";
            foreach (Object asset in selectedAsset)
            {
                //通过Assetdatabase类 获取 资源的路径
                string assetPath = AssetDatabase.GetAssetPath(asset);
                //截取路径当中的文件名 用于作为 StreamingAssets中的文件名
                string fileName = assetPath.Substring(assetPath.LastIndexOf('/'));
                //判断是否有.符号 如果有 证明有后缀 不处理
                if (fileName.IndexOf('.') != -1)
                    continue;
                AssetDatabase.CopyAsset(assetPath, "Assets/StreamingAssets" + fileName);
                FileInfo fileInfo = new FileInfo(Application.streamingAssetsPath + fileName);
                //拼接AB包信息到字符串中
                abCompareInfo += fileInfo.Name + " " + fileInfo.Length + " " + CreateABCompare.GetMD5(fileInfo.FullName);
                //用一个符号隔开多个AB包信息
                abCompareInfo += "|";
            }
            //去掉最后一个|符号 为了之后拆分字符串方便
            abCompareInfo = abCompareInfo.Substring(0, abCompareInfo.Length - 1);
            //将本地默认资源的对比信息 存入文件
            File.WriteAllText(Application.streamingAssetsPath + "/ABCompareInfo.txt", abCompareInfo);
            //刷新窗口
            AssetDatabase.Refresh();
        }

    }
}